home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / Draw / Sources / Ruler.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  8.3 KB  |  304 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Ruler.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "ODFDraw.hpp"
  11.  
  12. #ifndef CONSTANT_H
  13. #include "Constant.h"
  14. #endif
  15.  
  16. #ifndef RULER_H
  17. #include "Ruler.h"
  18. #endif
  19.  
  20. #ifndef FWFRAME_H
  21. #include "FWFrame.h"
  22. #endif
  23.  
  24. #ifndef FWRECSHP_H
  25. #include "FWRecShp.h"            // FW_CRectShape
  26. #endif
  27.  
  28. #ifndef FWTXTSHP_H
  29. #include "FWTxtShp.h"            // FW_CTextShape
  30. #endif
  31.  
  32. #ifndef FWLINSHP_H
  33. #include "FWLinShp.h"            // FW_CLineShape
  34. #endif
  35.  
  36. #ifndef FWCONTXT_H
  37. #include "FWContxt.h"
  38. #endif
  39.  
  40. #ifndef FWODGEOM_H
  41. #include "FWODGeom.h"
  42. #endif
  43.  
  44. // ----- OpenDoc Includes -----
  45.  
  46. #ifndef SOM_ODFacet_xh
  47. #include <Facet.xh>
  48. #endif
  49.  
  50. #ifndef SOM_ODWindow_xh
  51. #include <Window.xh>
  52. #endif
  53.  
  54. #ifndef SOM_ODShape_xh
  55. #include <Shape.xh>
  56. #endif
  57.  
  58. //========================================================================================
  59. // Runtime Informations
  60. //========================================================================================
  61.  
  62. #ifdef FW_BUILD_MAC
  63. #pragma segment odfdraw
  64. #endif
  65.  
  66. FW_DEFINE_AUTO(CRuler)
  67. FW_DEFINE_CLASS_M1(CRuler, FW_CSuperView)
  68.  
  69. const FW_ClassTypeConstant FW_LRuler = FW_TYPE_CONSTANT('r','u','l','r');
  70. FW_REGISTER_ARCHIVABLE_CLASS(FW_LRuler, CRuler, CRuler::Create, FW_CView::Read, CRuler::Destroy, FW_CView::Write)
  71.  
  72. //========================================================================================
  73. // CLASS CRuler
  74. //========================================================================================
  75.  
  76. const FW_Fixed kRulerExtent = FW_IntToFixed(1000);
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // CRuler::CRuler
  80. //----------------------------------------------------------------------------------------
  81.  
  82. CRuler::CRuler(Environment* ev) :
  83.     FW_CSuperView(ev),
  84.     fLineStyle(FW_kFixed0),
  85.     fFont(FW_GetTimesFontName(), FW_kPlain, FW_IntToFixed(10))
  86. {
  87. }
  88.  
  89. //----------------------------------------------------------------------------------------
  90. // CRuler::~CRuler
  91. //----------------------------------------------------------------------------------------
  92.  
  93. CRuler::~CRuler()
  94. {
  95. }
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // CRuler::ZoomFactorChanged
  99. //----------------------------------------------------------------------------------------
  100.  
  101. void CRuler::ZoomFactorChanged(Environment* ev, FW_Fixed zoomFactor)
  102. {
  103.     if (IsScrollingInY(ev))
  104.         fFont.SetFontSize(FW_IntToFixed(10) / zoomFactor);    
  105. }
  106.  
  107. //----------------------------------------------------------------------------------------
  108. // CRuler::Draw
  109. //----------------------------------------------------------------------------------------
  110.  
  111. void CRuler::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  112. {
  113.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  114.     
  115.     FW_CRect extent(FW_kZeroPoint, GetExtent(ev));
  116.         
  117.     extent.Intersection(FW_GetShapeBoundingBox(ev, invalidShape));
  118.  
  119.     FW_CRectShape::RenderRect(vc, extent, FW_kFill, FW_kWhiteEraseInk);
  120.  
  121.     if (IsScrollingInX(ev))
  122.         RenderHorizontalRuler(vc, extent);
  123.     else
  124.         RenderVerticalRuler(vc, extent);
  125. }
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // CRuler::RenderGradation
  129. //----------------------------------------------------------------------------------------
  130.  
  131. void CRuler::RenderGradation(FW_CViewContext& vc,
  132.                                 short gradation, 
  133.                                 const FW_CPoint& pos)
  134. {
  135.     FW_CString32 string;
  136.     string.ReplaceAllAsSignedDecimalInteger(gradation);
  137.  
  138.     FW_CTextShape::RenderText(vc, string,
  139.                             FW_CPoint(pos.x - FW_kFixedPos1, pos.y - FW_kFixedPos1),
  140.                             fFont,
  141.                             FW_kTextAlignRight | FW_kTextAlignBottom);
  142. }
  143.  
  144. //----------------------------------------------------------------------------------------
  145. // CRuler::RenderHorizontalRuler
  146. //----------------------------------------------------------------------------------------
  147.  
  148. void CRuler::RenderHorizontalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
  149. {    
  150.     FW_CLineShape::RenderLine(vc, 
  151.                                 FW_CPoint(visibleRect.left, kRulerWidth - FW_kFixedPos1), 
  152.                                 FW_CPoint(visibleRect.right, kRulerWidth - FW_kFixedPos1), 
  153.                                 FW_kNormalInk, fLineStyle);
  154.  
  155.     short h = FW_FixedToInt(kRulerWidth);
  156.     FW_Fixed full = FW_IntToFixed(2 * (h / 3));
  157.     FW_Fixed half = FW_IntToFixed(h / 2);
  158.     FW_Fixed third = FW_IntToFixed(h / 3);
  159.  
  160.     short short72 = 72;
  161.     
  162.     short gradation = FW_FixedToInt(visibleRect.left) / short72;
  163.     FW_Fixed minX = FW_IntToFixed(short72 * gradation);
  164.     FW_Fixed maxX = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.right + FW_IntToFixed(short72)) / short72));
  165.     
  166.     FW_Fixed fixed18 = FW_IntToFixed(18);
  167.     
  168.     short n = 0;
  169.     FW_Fixed height = full;
  170.     if (minX == FW_kFixed0)
  171.     {
  172.         minX = fixed18;
  173.         n = 1;
  174.         height = third;
  175.     }
  176.     
  177.     FW_CPoint start(minX, kRulerWidth);
  178.     FW_CPoint end(minX, kRulerWidth - height);
  179.     for (FW_Fixed x = minX; x <= maxX; x += fixed18)
  180.     {
  181.         FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, fLineStyle);
  182.         if (n == 0)
  183.             RenderGradation(vc, gradation, start);
  184.         
  185.         n++;
  186.         if (n == 4)
  187.         {
  188.             n = 0;
  189.             gradation++;
  190.             height = full;
  191.         }
  192.         else if (n == 2)
  193.             height = half;
  194.         else if (n == 1 || n == 3)
  195.             height = third;
  196.         
  197.         start.x += fixed18;
  198.         end.x += fixed18;
  199.         end.y = kRulerWidth - height;
  200.     }
  201. }
  202.  
  203. //----------------------------------------------------------------------------------------
  204. // CRuler::RenderVerticalRuler
  205. //----------------------------------------------------------------------------------------
  206.  
  207. void CRuler::RenderVerticalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
  208. {
  209.     FW_CLineShape::RenderLine(vc, 
  210.                                 FW_CPoint(kRulerWidth - FW_kFixedPos1, visibleRect.top), 
  211.                                 FW_CPoint(kRulerWidth - FW_kFixedPos1, visibleRect.bottom), 
  212.                                 FW_kNormalInk, fLineStyle);
  213.  
  214.     short h = FW_FixedToInt(kRulerWidth);
  215.     FW_Fixed full = FW_IntToFixed(2 * (h / 3));
  216.     FW_Fixed half = FW_IntToFixed(h / 2);
  217.     FW_Fixed third = FW_IntToFixed(h / 3);
  218.  
  219.     short short72 = 72;
  220.     
  221.     short gradation = FW_FixedToInt(visibleRect.top) / short72;
  222.     FW_Fixed minY = FW_IntToFixed(short72 * gradation);
  223. //    FW_Fixed minY = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.top) / short72));
  224.     FW_Fixed maxY = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.bottom + FW_IntToFixed(short72)) / short72));
  225.     
  226.     FW_Fixed fixed18 = FW_IntToFixed(18);
  227.     
  228.     short n = 0;
  229.     FW_Fixed width = full;
  230.     if (minY == FW_kFixed0)
  231.     {
  232.         minY = fixed18;
  233.         n = 1;
  234.         width = third;
  235.     }
  236.         
  237.     FW_CPoint start(kRulerWidth, minY);
  238.     FW_CPoint end(kRulerWidth - width, minY);
  239.     for (FW_Fixed y = minY; y <= maxY; y += fixed18)
  240.     {
  241.         FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, fLineStyle);
  242.         if (n == 0)
  243.             RenderGradation(vc, gradation, start);
  244.         
  245.         n++;
  246.         if (n == 4)
  247.         {
  248.             n = 0;
  249.             gradation++;
  250.             width = full;
  251.         }
  252.         else if (n == 2)
  253.             width = half;
  254.         else if (n == 1 || n == 3)
  255.             width = third;
  256.         
  257.         start.y += fixed18;
  258.         end.y += fixed18;
  259.         end.x = kRulerWidth - width;
  260.     }
  261. }
  262.  
  263. //----------------------------------------------------------------------------------------
  264. //    CRuler::Create
  265. //----------------------------------------------------------------------------------------
  266.  
  267. void* CRuler::Create(FW_CReadableStream& stream, FW_ClassTypeConstant type)
  268. {
  269. FW_UNUSED(stream);
  270. FW_UNUSED(type);
  271.     FW_SOMEnvironment ev;
  272.     return FW_NEW(CRuler, (ev));
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. //    CRuler::Destroy
  277. //----------------------------------------------------------------------------------------
  278.  
  279. void CRuler::Destroy(void* object, FW_ClassTypeConstant type)
  280. {
  281. FW_UNUSED(type);
  282.     CRuler* self = (CRuler*) object;
  283.     delete self;
  284. }
  285.  
  286. //----------------------------------------------------------------------------------------
  287. //    CRuler::Flatten
  288. //----------------------------------------------------------------------------------------
  289.  
  290. void CRuler::Flatten(Environment* ev, FW_CWritableStream& archive) const
  291. {
  292.     FW_CSuperView::Flatten(ev, archive);
  293. }
  294.  
  295. //----------------------------------------------------------------------------------------
  296. //    CRuler::InitializeFromStream
  297. //----------------------------------------------------------------------------------------
  298.  
  299. void CRuler::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  300. {
  301.     FW_CSuperView::InitializeFromStream(ev, stream);
  302. }
  303.  
  304.